<?php

// +----------------------------------------------------------------------
// | v1版本接口index
// +----------------------------------------------------------------------
// | Copyright (c) 2015-2024 http://www.yicmf.com, All rights reserved.
// +----------------------------------------------------------------------
// | Author: 微尘 <yicmf@qq.com>
// +----------------------------------------------------------------------

namespace app\controller\api\v3;

use app\event\cms\Document as DocumentEvent;
use app\forum\event\Post as PostEvent;
use app\forum\model\PostCategory;
use app\pay\model\PointExchange as PointExchangeModel;
use app\event\cms\Advertising as AdvertisingEvent;
use app\model\app\Modular as ModularModel;
use app\model\app\Modular2 as Modular2Model;
use app\model\store\Good as GoodModel;
use app\model\store\StoreFloor as StoreFloorModel;
use app\model\store\StoreNav as StoreNavModel;
use app\model\index\CreditcardProduct as CreditcardProductModel;
use app\model\cms\AdvertisingPopup as AdvertisingPopupModel;
use app\model\cms\AdvertisingSpace as AdvertisingSpaceModel;
use app\model\pay\PointProduct as PointProductModel;
use app\model\cms\Article as ArticleModel;
use app\model\cms\Notice as NoticeModel;
use think\facade\Db;
use think\facade\Log;

class Index extends Common
{

    /**
     * 首页
     */
    public function index()
    {
        $data = [];
        if ($this->is_examine) {
            $this->_examinInfo($data);
            $data['modular'] = Modular2Model::with('cover')->where('id', '<', '16')->where('status', 1)->order('sort ASC')->select();
        } else {
            if ($this->user) {
                $data['has_red'] = strtotime($this->user['create_time']) > strtotime('2024-12-10') ? 1 : 0;
                $data['has_red'] = 1;
                $data['show_red_model'] = 1;
            } else {
                $data['has_red'] = 0;
                $data['show_red_model'] = 0;
            }
            $data['modular'] = ModularModel::with('cover')->where('status', 1)->order('sort ASC')->select();
            $hot = CreditcardProductModel
                ::where('status', 1)
//                ->where('type', 1)
                ->where('is_hot', 1)
                ->field('bankName as name,type,logo,shortIntroduce,commission,virtualNum as virtual_num,type,commission_type,label1,label2,id,applyNum as apply_num')
                ->order('id desc')->select();
            if (count($hot) > 0) {
                $floors[] = [
                    'type' => 'single',
                    'content_type' => 'xyk',
                    'content_title' => '精选产品',
                    'title' => '精选产品',
                    'data' => $hot
                ];
            }
            $group[] =
                ['type' => 2,
                    'title' => '信用卡',
                    'content_type' => 'xyk',
                    'data' => CreditcardProductModel::where('type', 1)
                        ->where('is_new', 1)->field('bankName as name,logo,virtualNum as virtual_num,type,commission_type,shortIntroduce,commission,label1,label2,id,applyNum as apply_num')
                        ->limit(9)
                        ->where('status', 1)
                        ->order('updatetime desc')->select(),
                ];
            $group[] =
                ['type' => 2,
                    'title' => '贷款',
                    'content_type' => 'dk',
                    'data' => CreditcardProductModel::where('type', 2)
                        ->where('is_new', 1)->field('bankName as name,logo,virtualNum as virtual_num,type,commission_type,shortIntroduce,commission,label1,label2,id,applyNum as apply_num')
                        ->limit(9)
                        ->where('status', 1)
                        ->order('updatetime desc')->select(),
                ];
            $floors[] = [
                'type' => 'group',
                'title' => '新品推荐',
                'group' => $group
            ];
            $data['floors'] = $floors;
            $data['hot_search'] = ['中原银行', '信用卡'];
            $data['newTask'] = false;
            $data['notice'] = [
                'title' => '云副业会员可以抢先查看攻略',
                'link' => ''
            ];
        }

        //新手任务判断
//            if($this->auth->isLogin()){
//                $data['new']['type4'] = (new Task())->taskList(false);
//
//                $user = Db::name('user')->where('id',$this->auth->id)->find();
//                $day = time()-(3*24*60*60);
//                $task = Db::name('user_task')->where(['task_id'=>38,'user_id'=>$this->auth->id,'status'=>'2'])->find();
//                if($user['jointime'] > $day && empty($task)){
//                    $data['newTask'] = true;
//                }
//            }

        // 首页轮播
//            $data['modular'] = ModularModel::where('status',1)->order('sort ASC')->select();

        $data['modular_more'] = [];
        $data['banners'] = AdvertisingEvent::lists(1);
        $data['notice'] = AdvertisingEvent::lists(2)[0];
        $data['popup'] = AdvertisingEvent::lists(3);
        return $this->successAjax($data);
    }

    public function travel()
    {
        $data['banners'] = AdvertisingEvent::lists(1);

        $floor[] = ['name' => '携程酒店', 'image' => '', 'image_file' => 'cxjd'];
        $data['floors'][] = [
            'name' => '热门出行',
            'lists' => $floor,
        ];
        return $this->successAjax($data);
    }

    public function store()
    {
        $data['banners'] = AdvertisingEvent::lists(1);

        $data['lists'] = GoodModel::with('cover')->where('status', 1)->paginate();
        $floor[] = ['name' => '携程酒店', 'image' => '', 'image_file' => 'cxjd'];
//        $data['category'][] = [
//            'name'=>'POS机',
//            'img'=>'',
//        ];
        return $this->successAjax($data);
    }

    public function join()
    {
        $data['image_link'] = '';
        return $this->successAjax($data);
    }


    public function notice()
    {
        if ($this->request->isPost()) {

            $action = $this->request->param('deal');

            if ($action == 'read') {
                $msg = $this->user->messages()->where('id', $this->request->param('id'))->find();
                if ($msg) {
                    $msg['is_read'] = 1;
                    $msg->save();
                    return $this->successAjax('成功1');
                } else {
                    return $this->errorAjax('消息不存在');
                }
            } elseif ($action == 'delete') {
                $info = $this->user->messages()->where('id', $this->request->param('id'))->find();
                return $this->successAjax($info->delete());
            } else {
                return $this->successAjax($action);
            }

        } else {
            if ($this->user) {
                $sheet_id = $this->request->param('sheet_id', 1);
            } else {
                $sheet_id = 0;
            }

            if ($sheet_id == 1) {
                $data['lists'] = $this->user->messages()->order('create_time desc')->paginate();
            } elseif ($sheet_id == 2) {
                $data['lists'] = $this->user->messages()->order('create_time desc')->paginate();
            } else {
                $data['lists'] = NoticeModel::where('status', 1)
                    ->whereTime('start_time', '<', 'now')
                    ->whereTime('end_time', '>', 'now')->order('create_time DESC')->paginate();
            }
            $data['sheets'][] = [
                'name' => '公告',
                'num' => NoticeModel::where('status', 1)
                    ->whereTime('start_time', '<', 'now')
                    ->whereTime('end_time', '>', 'now')->count(),
            ];
            if ($this->user) {
                $data['sheets'][] = [
                    'name' => '站内信',
                    'num' => $this->user->messages()->where('is_read', 0)->count(),
                ];
            } else {
                $data['sheets'][] = [
                    'name' => '站内信',
                    'num' => 0,
                ];
            }

//            $data['sheets'][] = [
//                'name' => '项目通知',
//                'num' => 0,
//            ];
            return $this->successAjax($data);
        }

    }

    public function popup()
    {
        if ($this->request->isPost()) {
            $data['message'] = '参加成功';
        } else {
            $page = $this->request->get('page', '');
            if ($page) {
                $space = AdvertisingSpaceModel::where('page', $page)->find();
                if (!$space) {
                    $space = AdvertisingSpaceModel::create(['page' => $page, 'title' => $page, 'name' => $page]);
                }
                $data['lists'] = AdvertisingPopupModel::where('status', 1)->where('advertising_space_id', $space['id'])->select();
            } else {
                $data['lists'] = [];
            }
            $data['lists'] = [];
//            $data['lists'] = [
//                'id' => '11',
//                'activity_image' => 'https://v3.yunfuye.com/uploads/image/202412/10/6757dfd52fbf2.jpg',
//                'max_image' => '',
//                'show' => 'always',
//                'url' => '',
//                'title' => '参加活动',
//                'button_text' => '报名',
//                'height' => 700,
//                'width' => 500,
//                'desc' => '111参加活动参加活动参加活动参加活动参加活动参加活动参加活动参加活动11',
//                'open_url' => '',
//                'text_color' => '#FFF',
//            ];
        }


        return $this->successAjax($data);
    }

    public function pop()
    {
        $data['zpop'] = '';
        return $this->successAjax($data);
    }


    /**
     * 首页
     */
    public function search()
    {
        $data = [];
        $keyword = $this->request->get('keyword');
        if ($this->is_examine) {
            $data['lists'] = ArticleModel
                ::with('cover')->where('is_vip', 1)
//                ->where('is_hot',1)
                ->where('title|content|description', 'like', '%' . $keyword . '%')
                ->order('id desc')->paginate();
        } else {
            $data['lists'] = CreditcardProductModel
                ::where('status', 1)
                ->field('bankName as name,logo,shortIntroduce,virtualNum as virtual_num,type,commission_type,commission,label1,label2,id,applyNum as apply_num')
                ->where('bankName|shortIntroduce', 'like', '%' . $keyword . '%')
                ->order('id desc')->paginate();

        }
        return $this->successAjax($data);
    }

    protected function _examinInfo(&$data)
    {

        $vips = ArticleModel
            ::with('cover')->where('is_vip', 1)
//                ->where('is_hot',1)
            ->order('id desc')->select();
        if (count($vips) > 0) {
            $floors[] = [
                'type' => 'single',
                'content_type' => 'article',
                'content_title' => 'VIP专享',
                'title' => 'VIP专享',
                'data' => $vips
            ];
        }

        $group[] =
            ['type' => 2,
                'title' => '手工副业',
                'content_type' => 'article',
                'data' => ArticleModel
                    ::with('cover')->where('is_vip', 0)
                    ->order('id desc')->select(),
            ];
        $group[] =
            ['type' => 2,
                'title' => '副业推广',
                'content_type' => 'article',
                'data' => ArticleModel
                    ::with('cover')->where('is_vip', 0)
                    ->order('id desc')->select(),
            ];
        $floors[] = [
            'type' => 'group',
            'title' => '免费专区',
            'group' => $group
        ];
        $data['floors'] = $floors;
        $data['has_red'] = 0;
        $data['hot_search'] = ['哪里可以找到副业', '什么副业赚钱'];
        $data['notice'] = [
            'title' => '云副业会员可以抢先查看攻略',
            'link' => ''
        ];
    }

    public function credit_card()
    {
        $limit = 30;
        $modular_id = $this->request->param('modular_id', 1);
        $data['modular'] = ModularModel::with(['image', 'cover'])->where('id', $modular_id)->where('status', 1)->find();
        if ($modular_id == 9) {
            $modular_id = 2;
        }
        if ($modular_id == 8) {
            $modular_id = 3;
        }
        $order = $this->request->param('order', 'apply_num');
        $CreditCardList = CreditcardProductModel::where('status', 1)
            ->where('type', $modular_id)
//                ->order('sort desc,id desc')
            ->field('id,applyNum as apply_num,commission_type,virtualNum as virtual_num,bankName as name,type,logo,label1,label2,shortIntroduce,settlementType,commission')
            ->order(($order == 'commission' ? 'commission' : 'sort') . ' desc')
            ->paginate($limit);

        $data['lists'] = $CreditCardList;
        return $this->successAjax($data);
    }

    public function point()
    {
        $limit = 30;
        $data['modular'] = ModularModel::with(['image', 'cover'])->where('id', 2)->where('status', 1)->find();
        $order = $this->request->param('order', 'apply_num');
        $CreditCardList = PointProductModel::where('status', 1)->order(($order == 'commission' ? 'commission' : 'apply_num') . ' desc')
            ->where('price', '>', 0)
            ->paginate($limit);

        $data['lists'] = $CreditCardList;

        return $this->successAjax($data);
    }

    /**
     * 获取广告
     */
    public function adt()
    {

        $data = AdvertisingEvent::lists(1);
        return $this->successAjax($data);
    }

}
